package com.xm.servlet.admin;

import com.xm.biz.UserAddressBiz;
import com.xm.biz.impl.UserAddressBizImpl;
import com.xm.po.UserAddress;
import com.xm.util.JsonUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-09-19 9:32
 * @desc 用户地址处理器
 **/
@WebServlet("/useraddress")
public class UserAddressServlet extends HttpServlet {
    //初始化数据源
    UserAddressBiz userAddressBiz = new UserAddressBizImpl();
    UserAddress userAddress = new UserAddress();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String type = req.getParameter("type");
        /*用户地址管理(查询)*/
        if (type.equals("userAddress")){
            userAddress(req, resp);
        }
        /*用户地址管理(删除)*/
        else if (type.equals("userAddressDelete")){
            userAddressDelete(req, resp);
        }
        /*修改用户地址(提交按钮)*/
            else if (type.equals("userAddressUpdate")){
            try {
                userAddressUpdate(req, resp);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        /*查询用户地址(修改按钮)*/
        else if (type.equals("userAddressByIdSelect")){
            userAddressByIdSelect(req, resp);
        }
        /*添加用户地址信息*/
        else if (type.equals("userAddressAdd")){
            try {
                userAddressAdd(req, resp);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    //用户地址管理(查询)
    public void userAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserAddress> list = userAddressBiz.list();
        request.setAttribute("UserAddress",list);
        //request.getRequestDispatcher("admin/useraddress.jsp").forward(request,response);
        request.getRequestDispatcher("admin/useraddress.jsp").forward(request,response);
    }
    //用户地址管理(删除)(有问题)
    public void userAddressDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String addressId = request.getParameter("addressId");
        boolean delete = userAddressBiz.delete(addressId);
        PrintWriter out = response.getWriter();
        if (delete==true){
            out.print("<script>location.href='useraddress?type=userAddressDelete'</script>");
            out.flush();
            out.close();
        }else {
            out.print("<script>alert('删除失败！');location.href='useraddress?type=userAddressDelete'</script>");
            out.flush();
            out.close();
        }
    }
    //查询用户地址(修改按钮)
    public void userAddressByIdSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addressId = req.getParameter("addressId");
        UserAddress userAddress = userAddressBiz.selectById(Integer.valueOf(addressId));
        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(userAddress));
        out.flush();
        out.close();
    }
    //修改用户地址
    public void userAddressUpdate(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        String addressId = req.getParameter("addressId");
        userAddress.setAddressId(Integer.valueOf(addressId));
        //json转换日期格式
        DateConverter converter = new DateConverter();
        converter.setPattern(new String("yyyy-MM-dd HH:mm:ss"));
        ConvertUtils.register(converter, Date.class);

        BeanUtils.populate(userAddress,req.getParameterMap());
        boolean update = userAddressBiz.update(userAddress);
        PrintWriter out = resp.getWriter();
        if (update==true){
            out.print("<script>location.href='useraddress?type=userAddress'</script>");
            out.flush();
            out.close();
        }else {
            out.print("<script>alert('修改失败！');location.href='useraddress?type=userAddress'</script>");
            out.flush();
            out.close();
        }
    }
    //添加用户地址信息
    public void userAddressAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //json转换日期格式
        DateConverter converter = new DateConverter();
        converter.setPattern(new String("yyyy-MM-dd HH:mm:ss"));
        ConvertUtils.register(converter, Date.class);
        //获取所有信息
        BeanUtils.populate(userAddress,req.getParameterMap());
        //将时间赋值为当前时间
        userAddress.setCreateTime(new Date());
        //调用添加功能
        boolean add = userAddressBiz.add(userAddress);
        PrintWriter out = resp.getWriter();
        if (add==true){
            out.print("<script>location.href='useraddress?type=userAddress'</script>");
            out.flush();
            out.close();
        }else {
            out.print("<script>alert('添加失败！');location.href='useraddress?type=userAddress'</script>");
            out.flush();
            out.close();
        }
    }
}
